//
//  UIHelper.m
//  YueMi
//
//  Created by Rainbow on 4/29/11.
//  Copyright 2011 iTotemStudio. All rights reserved.
//

#import "UIHelper.h"


@implementation UIHelper

+ (UIView *)newMaskViewForView:(UIView *)view alpha:(CGFloat)alpha{
	UIView *maskView = [[UIView alloc] initWithFrame:view.bounds];
	maskView.backgroundColor = [UIColor blackColor];
	maskView.alpha = alpha;
	return maskView;
}

+ (UIView *)addMaskToView:(UIView *)view alpha:(CGFloat)alpha{
	UIView *maskView = [[UIView alloc] initWithFrame:view.bounds];
	maskView.backgroundColor = [UIColor colorWithWhite:0 alpha:alpha];
	maskView.tag = kTagCommonMaskView;
	[view addSubview:maskView];
	return [maskView autorelease];
}

+ (void)removeMaskForView:(UIView *)view{
	UIView *maskView = [view viewWithTag:kTagCommonMaskView];
	if( maskView!=nil ){
		[maskView removeFromSuperview];
	}
}

+ (void)showGlobalMaskWithShadow:(BOOL)hasShadow{
	UIWindow *window = [UIApplication sharedApplication].keyWindow;
	UIView *maskView = [window viewWithTag:kTagGlobalMaskView];
	if( maskView != nil ){
		[maskView removeFromSuperview];
	}
	maskView = [[UIView alloc] initWithFrame:window.bounds];
	maskView.backgroundColor = [UIColor clearColor];
	if( hasShadow )
		maskView.backgroundColor = [UIColor colorWithWhite:0 alpha:0.5];
	maskView.tag = kTagGlobalMaskView;
	[window addSubview:maskView];
	[maskView release];
}

+ (void)hideGlobalMask{
	UIWindow *window = [UIApplication sharedApplication].keyWindow;
	UIView *maskView = [window viewWithTag:kTagGlobalMaskView];
	if( maskView != nil ){
		[maskView removeFromSuperview];
	}
}

+ (UIImage *)newHalfImage:(UIImage *)image side:(NSInteger)kHalfImage{
	CGImageRef imageRef = image.CGImage;
	CGImageRef halfImageRef;
	if( kHalfImage == kTypeHalfImageLeft ){
		halfImageRef = CGImageCreateWithImageInRect(imageRef,CGRectMake(0, 0, image.size.width/2, image.size.height));
	}else{
		halfImageRef = CGImageCreateWithImageInRect(imageRef,CGRectMake(image.size.width/2, 0, image.size.width/2, image.size.height));
	}
	UIImage *halfImage = [[UIImage alloc] initWithCGImage:halfImageRef];
	CGImageRelease(halfImageRef);

	return halfImage;
}

+ (CGSize)getScaledImageSize:(CGSize)imgSize frameSize:(CGSize)frameSize{
	CGFloat newWidth;
	CGFloat newHeight;
	BOOL isFitWidth = NO;
	if( [[self class] getImageTypeWithSize:imgSize]==kTypeImageRectangleH ){
		if( [[self class] getImageTypeWithSize:frameSize]==kTypeImageRectangleH && frameSize.width/frameSize.height>imgSize.width/imgSize.height ){
			isFitWidth = YES;
		}
	}else if( [[self class] getImageTypeWithSize:imgSize]==kTypeImageRectangleV ){
		isFitWidth = YES;
		if( [[self class] getImageTypeWithSize:frameSize]==kTypeImageRectangleH && frameSize.height/frameSize.width>imgSize.height/imgSize.width ){
			isFitWidth = NO;
		}
	}else{
		if( [[self class] getImageTypeWithSize:frameSize]==kTypeImageRectangleH ){
			isFitWidth = YES;
		}
	}
	if( isFitWidth ){
		newWidth = frameSize.width;
		newHeight = imgSize.height/imgSize.width*newWidth;
	}else{
		newHeight = frameSize.height;
		newWidth = imgSize.width/imgSize.height*newHeight;
	}
	return CGSizeMake(newWidth, newHeight);
}

+ (NSInteger)getImageTypeWithSize:(CGSize)imgSize{
	NSInteger type;
	if( imgSize.width>imgSize.height ){
		type = kTypeImageRectangleH;
	}else if( imgSize.width<imgSize.height ){
		type = kTypeImageRectangleV;
	}else{
		type = kTypeImageSquare;
	}
	return type;
}

+ (UILabel *)newLabelWithFrame:(CGRect)frame
						  text:(NSString *)text
					 textColor:(UIColor *)textColor
						  font:(UIFont *)font{
    UILabel *label = [[UILabel alloc] initWithFrame:frame];
	label.text = text;
	label.textColor = textColor;
	label.backgroundColor = [UIColor clearColor];
	label.textAlignment = UITextAlignmentLeft;
	label.font = font;
    return label;
}

+ (UIButton *)newTextButtonWithFrame:(CGRect)frame
							   title:(NSString *)title 
						  titleColor:(UIColor *)titleColor
						 tappedColor:(UIColor *)tappedColor
								font:(UIFont *)font
							  target:(id)target 
							  action:(SEL)selector{
	UIButton *button = [[UIButton alloc] initWithFrame:frame];
	button.backgroundColor = [UIColor clearColor];
	[button setTitle:title forState:UIControlStateNormal];
	[button setTitleColor:titleColor forState:UIControlStateNormal];
	[button setTitleColor:tappedColor forState:UIControlStateHighlighted];
	button.titleLabel.font = font;
	[button addTarget:target action:selector forControlEvents:UIControlEventTouchUpInside];
	return button;
}

+ (UIButton *)newImageButtonWithFrame:(CGRect)frame
								image:(UIImage *)image
						  tappedImage:(UIImage *)tappedImage
							   target:(id)target 
							   action:(SEL)selector{
	UIButton *button = [[UIButton alloc] initWithFrame:frame];
	button.backgroundColor = [UIColor clearColor];
	[button setImage:image forState:UIControlStateNormal];
	[button setImage:tappedImage forState:UIControlStateHighlighted];
	[button addTarget:target action:selector forControlEvents:UIControlEventTouchUpInside];
	return button;
}

+ (UIButton *)newActionButtonWithFrame:(CGRect)frame
								 title:(NSString *)title 
							titleColor:(UIColor *)titleColor
						   tappedColor:(UIColor *)tappedColor
								  font:(UIFont *)font
								target:(id)target 
								action:(SEL)selector{
	UIButton *button = [[UIButton alloc] initWithFrame:frame];
	button.backgroundColor = [UIColor clearColor];
	[button setTitle:title forState:UIControlStateNormal];
	[button setTitleColor:titleColor forState:UIControlStateNormal];
	[button setTitleColor:tappedColor forState:UIControlStateHighlighted];
	button.titleLabel.font = font;
	[button addTarget:target action:selector forControlEvents:UIControlEventTouchUpInside];
	[button setBackgroundImage:[[UIImage imageNamed:@"btnLogin.png"] stretchableImageWithLeftCapWidth:10 topCapHeight:0] forState:UIControlStateNormal];
	[button setBackgroundImage:[[UIImage imageNamed:@"btnLogin.png"] stretchableImageWithLeftCapWidth:10 topCapHeight:0] forState:UIControlStateHighlighted];
	
	return button;
}

+ (void)showMsgAlertWithTitle:(NSString *)title message:(NSString *)msg{
	UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:title message:msg delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil];
	[alertView show];
	[alertView release];
}

+ (void)showProcessIndicatorWithView:(UIView *)view atPoint:(CGPoint)point hasMask:(BOOL)hasMask style:(UIActivityIndicatorViewStyle)style
{
	if( hasMask ){
		UIView *maskView = [[UIView alloc] initWithFrame:view.bounds];
		maskView.backgroundColor = [UIColor colorWithWhite:0 alpha:0.5];
		maskView.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;
		maskView.tag = kTagIndicatorMaskView;
		[view addSubview:maskView];
		[maskView release];
	}
	UIActivityIndicatorView *indicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:style];
	indicator.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin|UIViewAutoresizingFlexibleRightMargin|UIViewAutoresizingFlexibleTopMargin|UIViewAutoresizingFlexibleBottomMargin;
	indicator.center = point;
	indicator.tag = kTagCommonIndicatorView;
	[view addSubview:indicator];
	[indicator release];
	[indicator startAnimating];
}
+ (void)showProcessIndicatorWithView:(UIView *)view atPoint:(CGPoint)point hasMask:(BOOL)hasMask{
	[self showProcessIndicatorWithView:view atPoint:point hasMask:hasMask style:UIActivityIndicatorViewStyleWhite];
}
+ (void)hideProcessIndicatorWithView:(UIView *)view{
	UIView *maskView = [view viewWithTag:kTagIndicatorMaskView];
	if( maskView != nil ){
		[maskView removeFromSuperview];
	}
	UIView *indicator = [view viewWithTag:kTagCommonIndicatorView];
	if( indicator != nil ){
		[indicator removeFromSuperview];
	}
}

+ (void)roundCenter:(UIView *)view{
	view.center = CGPointMake(round(view.center.x), round(view.center.y));
}

+ (void)addAnimationShow:(UIView *)viewToAddAnimation{
	viewToAddAnimation.alpha = 0;
	[UIView beginAnimations:nil context:nil];
	[UIView setAnimationDuration:0.5];
	[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
	viewToAddAnimation.alpha = 1;
	[UIView commitAnimations];
}

+ (void)addAnimationHalfShow:(UIView *)viewToAddAnimation{
    viewToAddAnimation.alpha = 0;
	[UIView beginAnimations:nil context:nil];
	[UIView setAnimationDuration:0.5];
	[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
	viewToAddAnimation.alpha = 0.65;
	[UIView commitAnimations];
}

+ (void)addAnimationFade:(UIView *)viewToAddAnimation{
	[UIView beginAnimations:nil context:nil];
	[UIView setAnimationDuration:0.5];
	[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
	viewToAddAnimation.alpha = 0;
	[UIView commitAnimations];
}

+ (void)addAnimationClick:(UIView *)viewToAddAnimation{
	viewToAddAnimation.alpha = 0;
	[UIView beginAnimations:nil context:nil];
	[UIView setAnimationDuration:0.2];
	[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
	viewToAddAnimation.alpha = 1;
	[UIView commitAnimations];
}

+ (void)addAnimationSlideUp:(UIView *)viewToAddAnimation{
	[UIView beginAnimations:nil context:nil];
	[UIView setAnimationDuration:0.5];
	[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
	viewToAddAnimation.center = CGPointMake(viewToAddAnimation.center.x, viewToAddAnimation.center.y-viewToAddAnimation.frame.size.height);
	[UIView commitAnimations];
}

+ (void)addAnimationSlideDown:(UIView *)viewToAddAnimation{
	[UIView beginAnimations:nil context:nil];
	[UIView setAnimationDuration:0.5];
	[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
	viewToAddAnimation.center = CGPointMake(viewToAddAnimation.center.x, viewToAddAnimation.center.y+viewToAddAnimation.frame.size.height);
	[UIView commitAnimations];
}

+ (void)addAnimationSlideLeft:(UIView *)viewToAddAnimation{
	[UIView beginAnimations:nil context:nil];
	[UIView setAnimationDuration:0.5];
	[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
	viewToAddAnimation.center = CGPointMake(viewToAddAnimation.center.x-viewToAddAnimation.frame.size.width, viewToAddAnimation.center.y);
	[UIView commitAnimations];
}

+ (void)addAnimationSlideRight:(UIView *)viewToAddAnimation{
	[UIView beginAnimations:nil context:nil];
	[UIView setAnimationDuration:0.5];
	[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
	viewToAddAnimation.center = CGPointMake(viewToAddAnimation.center.x+viewToAddAnimation.frame.size.width, viewToAddAnimation.center.y);
	[UIView commitAnimations];
}

+ (void)removeSubviews:(UIView *)theView{
	for( NSInteger i=0;i<[theView.subviews count];i++ ){
		UIView *view = (UIView *)[theView.subviews objectAtIndex:i];
		[view removeFromSuperview];
		i--;
	}
}
+(UIImage*)getImage:(UIImage*)pImage width:(float)width height:(float)heigth
{
    CGSize size = CGSizeMake(width, heigth);
    UIGraphicsBeginImageContext(size);
    [pImage drawInRect:CGRectMake(0,0, size.width, size.height)];
    pImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return pImage;
}

+ (UIImage*)getImage:(UIImage *)pImage scale:(float)scale
{
    CGSize size = CGSizeMake(pImage.size.width*scale, pImage.size.height *scale);
    UIGraphicsBeginImageContext(size);
    [pImage drawInRect:CGRectMake(0,0, size.width, size.height)];
    pImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return pImage;
}

@end
